package com.irishud.hudnavi.wifi;

/**
 * Created by Administrator on 2016/10/12.
 */
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class WifiReceiver extends BroadcastReceiver{
    public interface WifiConnectionListener{
        void OnWifiOpened();
        void OnWifiClosed();
        void OnWifiConnectionError(String ssid);
        void OnWifiConnected(String ssid);
        void OnWifiDisconnected(String ssid);
    }
    List<WifiConnectionListener> listeners  = new ArrayList<>();

    String TAG = "WifiReceiver";
    static WifiReceiver instance = new WifiReceiver();
    public static WifiReceiver getInstance(){
        return  instance;
    }
    private WifiReceiver(){}

    public void addWifiConnectionListener(WifiConnectionListener listener){
       synchronized (listeners){
           if(!listeners.contains(listener)){
               listeners.add(listener);
           }
       }
    }

    public void removeWifiConnectionListener(WifiConnectionListener listener){
        synchronized (listeners){
            if(listeners.contains(listener)){
                listeners.remove(listener);
            }
        }
    }

    public void notifyWifiOpen(){
        for(WifiConnectionListener listener : listeners){
            listener.OnWifiOpened();
        }
    }

    public void notifyWifiClose(){
        for(WifiConnectionListener listener : listeners){
            listener.OnWifiClosed();
        }
    }

    public void notifyWifiConnected(String ssid){
        for(WifiConnectionListener listener : listeners){
            listener.OnWifiConnected(ssid);
        }
    }

    public void notifyWifiConnectionDisconnected(String ssid){
        for(WifiConnectionListener listener : listeners){
            listener.OnWifiDisconnected(ssid);
        }
    }

    String currentSSID = "";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if(intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)){
            //signal strength changed
        } else if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){//wifi连接上与否
            Log.e(TAG,"-------wifi 网络状态改变");
            NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if(info.getState().equals(NetworkInfo.State.DISCONNECTED)){
                Log.w(TAG,"wifi网络连接断开");
                notifyWifiConnected(currentSSID);
            }else if(info.getState().equals(NetworkInfo.State.CONNECTED)){
                WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                currentSSID = wifiInfo.getSSID();
                //获取当前wifi名称
                Log.i(TAG,"---------------> 连接到热点: " + wifiInfo.getSSID());
            }
        } else if(intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)){//wifi打开与否
            int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
            if(wifistate == WifiManager.WIFI_STATE_DISABLED){
                Log.i(TAG,"系统关闭wifi");
            }
            else if(wifistate == WifiManager.WIFI_STATE_ENABLED){
                Log.i(TAG,"系统开启wifi");
            }
        }else if(intent.getAction().equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)){
            int state = intent.getIntExtra("wifi_state",  0);
            Log.i(TAG, "===> wifi supplicant state changed!");
        }
    }
}